﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using FX.CC.Andon.Entity.BasicDataManage;
using FX.CC.Andon.Service.BasicDataManage;
using FX.CC.Andon.Utility.Json;

namespace FX.CC.Andon.Business.BasicDataManage
{
    public class ETypeMailBusiness
    {
        private readonly ETypeMailService _dal = new ETypeMailService();

        public string GetETypeMailListById(ETypeMailQcEntity qcmodel)
        {
            var total = _dal.GetETypeMailListByIdCount(qcmodel);
            var list = _dal.GetETypeMailListById(qcmodel);
            return JsonHelper.Encode(total, list);
        }

        public bool AddETypeMail(string data)
        {
            var result = false;
            var rows = (ArrayList)JsonHelper.Decode(data);
            var list = new List<ETypeMailEntity>();
            foreach (Hashtable row in rows)
            {
                var eTypeCode = row["ETypeCode"].ToString();
                var arryList = (ArrayList)row["arrList"];

                list.AddRange(from Hashtable item in arryList
                              select new ETypeMailEntity
                              {
                                  RowID = Guid.NewGuid(),
                                  ETypeCode = eTypeCode,
                                  MailAddr = item["MailAddr"].ToString()
                              });
            }
            if (list.Count > 0)
            {
                result = _dal.AddETypeMail(list);
            }
            return result;
        }

        public bool ModifyETypeMail(string data)
        {
            var result = false;
            var rows = (ArrayList)JsonHelper.Decode(data);
            var list = new List<ETypeMailEntity>();
            var eTypeCode = "";
            foreach (Hashtable row in rows)
            {
                eTypeCode = row["ETypeCode"].ToString();
                var arryList = (ArrayList)row["arrList"];
                list.AddRange(from Hashtable item in arryList
                              select new ETypeMailEntity
                              {
                                  RowID = Guid.NewGuid(),
                                  ETypeCode = eTypeCode,
                                  MailAddr = item["MailAddr"].ToString()
                              });
            }

            if (list.Count > 0)
            {
                _dal.RemoveByETypeCode(eTypeCode);
                result = _dal.AddETypeMail(list);
            }
            return result;
        }

        public string GetETypeMailListByETypeCode(string id)
        {
            return JsonHelper.Encode(_dal.GetETypeMailListByETypeCode(id));
        }

        public bool RemoveByRowId(string id)
        {
            return _dal.RemoveByRowId(id);
        }
    }
}
